package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortCartData;
import com.yanqu.road.entity.beautyescort.EscortServerGroup;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.server.manger.EscortServerGroupMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_VIEW_USER, desc = "查看玩家（从战报）")
public class CrossBeautyEscortViewUserCmd extends CrossCommand {

    long userServerId;
    long userId;
    long playerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.BeautyEscortViewUserReqMsg reqMsg = BeautyEscortProto.BeautyEscortViewUserReqMsg.parseFrom(packet.getBytes());
        userServerId = reqMsg.getServerId();
        userId = reqMsg.getUserId();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        //是否是同分组
        EscortServerGroup group = EscortServerGroupMgr.getServerGroupByServerId(serverId);
        if(group != null && group.getEscortState() == 1){
            if(!group.getServerList().contains(userServerId)){
                ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_IN_GROUP;
                return;
            }
        }else if(serverId != userServerId) {
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_IN_GROUP;
            return;
        }

        CrossEscortPlayerWrap playerWrap = CrossBeautyEscortMgr.getEscortPlayer(userServerId, userId);
        if(playerWrap == null){
            ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_FOUND;
            return;
        }
        BeautyEscortProto.BeautyEscortViewUserRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortViewUserRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(playerWrap.getUserBaseInfo()));
        for (EscortCartData cartData : playerWrap.getCartDataMap().values()) {
            if(cartData.getEnemy().contains(playerId)){
                continue;
            }
            if(!playerWrap.canRob(cartData, ConfigMgr.getMaxRobTimesInLoss(), playerId)){
                continue;
            }
            BeautyEscortProto.BeautyEscortCartFindTemp.Builder cartBuilder = CrossBeautyEscortMgr.getSearchCartBuilder(cartData);
            respMsg.addCart(cartBuilder);
        }
        respMsg.setTotalGlamour(playerWrap.getUserData().getTotalGlamour());
        respMsg.setUserId(playerWrap.getUserData().getUserId());
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_VIEW_USER, respMsg));
    }



    @Override
    public void sendData() {
        if(ret != 0){
            BeautyEscortProto.BeautyEscortViewUserRespMsg.Builder respMsg = BeautyEscortProto.BeautyEscortViewUserRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_BEAUTY_ESCORT_VIEW_USER, respMsg));
        }
    }
}
